$("#addSuitBtn").click(function () {
    $.ajax({
        url: "/flytest/test/testsuit",
        type: "post",
        data: $("#testsuit_add_form").serialize(),
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                window.location.href = "/flytest/test/testsuits"
            }
        }
    });
});


$("#updateSuitBtn").click(function () {
    $.ajax({
        url: "/flytest/test/testsuit",
        type: "put",
        data: $("#testsuit_update_form").serialize(),
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                window.location.href = "/flytest/test/testsuits"
            }
        }
    });
});

// 设置当前测试集信息
function activeCurrentProjectOption(suitId) {
    setProject('belongProject_suit_u');
    $.ajax({
        url: "/flytest/test/testsuit?id=" + suitId,
        type: "get",
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                console.log(result.testsuit.belongTo);
                $("#testsuitId_u").val(result.testsuit.testSuitId);
                $("#suitName_u").val(result.testsuit.suitName);
                // 设置所属项目被选中
                $("#belongProject_suit_u").val(result.testsuit.belongTo);
            }
        }
    });
}

/**
 * 设置所属项目下拉列表内容
 * @param elementId
 */
function setProject(elementId) {
    $.ajax({
        url: "/flytest/proj/projects",
        type: "get",
        async: false,
        success: function (result) {
            console.log(result);
            if (result.code === '200') {
                //设置project内容到下拉框
                var projects = result.projects;
                console.log(projects);
                var select = document.getElementById(elementId);
                var option = "";
                for (var i = 0; i < projects.length; i++) {
                    option = option + "<option value=\"{projectId}\">{projectName}</option>".signMix(projects[i])
                }
                //console.log(option);
                select.innerHTML = option;
            }
        }
    });
}

function deleteTestSuit(testSuitId) {
    bootbox.confirm({
        message: "确认删除吗？",
        buttons: {
            confirm: {
                label: 'Yes',
                className: 'btn-success'
            },
            cancel: {
                label: 'No',
                className: 'btn-danger'
            }
        },
        callback: function (result) {
            if (result) {
                $.ajax({
                    url: "/flytest/test/testsuit?id=" + testSuitId,
                    type: "delete",
                    success: function (result) {
                        if (result.code === '200') {
                            window.location.href = "/flytest/test/testsuits"
                        }
                    }
                })
            }
        }
    });
}


var selectedTestSuit = "";

/**
 * 点击关联用例按钮，弹出前事件
 * @param testSuitId
 */
function linkTestCaseShow(testSuitId) {
    selectedTestSuit = testSuitId;
    $.ajax({
        url: '/flytest/test/testcase/all',
        type: 'get',
        dataType: 'json',
        success: function (res) {
            if (res.code === '200') {
                // console.log(res.testcases);
                var testcases = res.testcases;
                var table_body = "";

                for (let i = 0; i < testcases.length; i++) {
                    let testcase = testcases[i];
                    var testcaseId = "<td class=\"text-center\">{testcaseId}</td>".signMix(testcase);
                    var testcaseName = "<td class=\"text-center\">{testcaseName}</td>".signMix(testcase);
                    var description = "<td class=\"text-center\">{description}</td>".signMix(testcase);
                    var interfaceId = "<td class=\"text-center\">{interfaceId}</td>".signMix(testcase);
                    var testSuitId = "<td class=\"text-center\">{testSuitId}</td>".signMix(testcase);
                    var cid = "<td class=\"text-center\"><div class=\"animated-checkbox\"><label><input type=\"checkbox\" name=\"caseid\" value=\"{testcaseId}\"><span class=\"label-text\">关联</span></label></div></td>\n".signMix(testcase);
                    var td = testcaseId + testcaseName + description + interfaceId + testSuitId + cid;
                    var tr = "<tr>{0}</tr>".signMix(td);
                    table_body = table_body + tr;
                }
                document.getElementById("link_tbody").innerHTML = table_body;
                drawDataTable("#linkTable");
            }
        }
    });
}

/**
 * 链接测试测试用例按钮点击事件
 */
function linkTestCase() {
    let boxList = document.getElementsByName("caseid");
    check_val = []
    for (k in boxList) {
        if (boxList[k].checked) {
            check_val.push(boxList[k].value);
        }
    }
    var cid = check_val.join(':');
    var sid = selectedTestSuit;

    var url = '/flytest/test/testcase/link?cid=' + cid + "&sid=" + sid;
    $.ajax({
        url: url,
        type: 'get',
        dataType: 'json',
        success: function (res) {
            if (res.code === '200') {
                window.location.href = '/flytest/test/testsuits';
            }
        }
    });

}

function runTestSuit(id) {
    $("#fakeLoader").fakeLoader({
        timeToHide: 1000000, //加载效果的持续时间
        zIndex: "999",
        spinner: "spinner2",//可选值 'spinner1', 'spinner2', 'spinner3', 'spinner4', 'spinner5', 'spinner6', 'spinner7' 对应有7种效果
        bgColor: "#009688", //加载时的背景颜色
        // imagePath:"yourPath/customizedImage.gif" //自定义的加载图片
    });

    $.ajax({
        url: '/flytest/test/testsuit/run?id=' + id,
        type: 'get',
        dataType: 'json',
        success: function (res) {
            if (res.code === '200') {
                console.log("run testsuit complete,report id :" + res.report);
                // 弹出显示报告提示框
                // swal("测试完成，请查看测试报告");

                const url = "/flytest/test/report?id=" + res.report;
                showReport(url);

            } else {
                window.location.href = "/flytest/test/testsuits"
                swal("测试失败，请联系管理员");
            }
        }
    })
}

function showReport(report_url) {
    bootbox.confirm({
        message: "测试完成，是否查看测试报告？",
        buttons: {
            confirm: {
                label: 'Yes',
                className: 'btn-success'
            },
            cancel: {
                label: 'No',
                className: 'btn-danger'
            }
        },
        callback: function (result) {
            if (result) {
                window.location.href = report_url;
            } else {
                window.location.href = "/flytest/test/testsuits"
            }
        }
    });
}